import { action, makeObservable, observable, runInAction } from 'mobx';

import WithCommonStore from 'Core/common/WithCommonStore';

import type { ActionRequestCanceler } from 'Typings/common';

/**
 * `wrapper class` for **common create** store
 * @props defaultCreateFormData the default `form` data of the `filter` data for the **create**
 * @props createFormData the `form` data of the `filter` data for the **create**
 * @props createRequesting the `request status` of creating new target
 * @function createCanceler the `axios request canceler` of creating new target
 * @function destoryCreateFormData `clear` the `data` of the **create form** related
 * @function setCreateFormData `set` the data of the `create form`
 * @return `Class WithCreateForm`
 */
class WithCreateForm<Form = Record<string, unknown>> extends WithCommonStore {
    @observable defaultCreateFormData: Form = {} as Form;

    @observable createFormData: Form = {} as Form;

    @observable createRequesting = true;

    @observable.ref createCanceler: ActionRequestCanceler = () => {};

    constructor() {
        super();
        makeObservable(this);
    }

    @action destoryCreateFormData: WithReturnFuc<[Form?], unknown> = data => {
        runInAction(() => {
            this.setCreateFormData(data ?? this.defaultCreateFormData);
            this.createRequesting = true;
            this.createCanceler = () => {};
        });
    };

    @action setCreateFormData: WithReturnFuc<[Form], unknown> = data => {
        this.createFormData = data;
    };

    @action setDefaultCreateFormData: WithReturnFuc<[Form], unknown> = data => {
        this.defaultCreateFormData = data;
    };
}

export default WithCreateForm;
